﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ElevatorSimCSharp
{
    class Interactions
    {
        public static void movePeopleToElevator(Building building)
        {
            Elevator[] elevatorArray = building.getElevators();
            foreach (Elevator elevator in elevatorArray)
            {
                short currentFloor = elevator.getCurrentFloor();
                sbyte direction = elevator.getDirection();
                List<Person> elevatorPeople = elevator.getPersonsList();
                foreach (Person person in elevatorPeople)
                {
                    if (currentFloor == person.getWantToGoTo())
                    {
                        elevatorPeople.Remove(person);
                        elevator.decrementNumberOfPeopleInElevator();
                        Person.numberOfPeopleWhoveTakenElevator++;
                    }
                }
                elevator.setPersonsList(elevatorPeople);

                List<Person> floorPeople = building.getCurrentFloorPeopleList(elevator.getCurrentFloor());
                while (elevator.getNumberOfPeopleInElevator() < elevator.getMaxNumberOfPeople())
                {
                    foreach (Person person in floorPeople)
                    {
                        if (person.getWantToGoTo() - elevator.getCurrentFloor() > 0 && elevator.getDirection() > 0)
                        {
                            elevator.incrementNumberOfPeopleInElevator();
                            elevator.addPerson(person);
                            floorPeople.Remove(person);
                        }
                        else if (person.getWantToGoTo() - elevator.getCurrentFloor() < 0 && elevator.getDirection() < 0)
                        {
                            elevator.incrementNumberOfPeopleInElevator();
                            elevator.addPerson(person);
                            floorPeople.Remove(person);
                        }
                    }
                }
            }
        }

        public static void createRandomPerson(Building building)
        {
            Random random = new Random();
            if (random.Next(0, 10) >= 8)
            {
                Person person = new Person();
                short floor = (short)random.Next((int)building.getLowestFloor(), (int)building.getHighestFloor());
                do
                {
                    person.setWantToGoTo((byte)random.Next(building.getLowestFloor(), building.getHighestFloor()));
                } while (person.getWantToGoTo() == floor);
                building.putPersonOnFloor(person, floor);
            }
        }

        public static void findLastFloorToGoTo(Elevator[] elevators, Floor[] floors) {
            foreach (Elevator elevator in elevators) {
                List<Person> people = elevator.getPersonsList();
                if (elevator.getDirection() > 0) {
                    short highestFloor = elevator.getLastFloorToGoTo();
                    foreach (Person person in people) {
                        if (person.getWantToGoTo() > elevator.getLastFloorToGoTo()) {
                            highestFloor = person.getWantToGoTo();
                        }
                    }
                    short highestFloorWithPeople = Interactions.checkFloorsForPeopleAboveElevator(floors, elevator);
                    if (highestFloor > highestFloorWithPeople) {
                            elevator.setLastFloorToGoTo(highestFloor);
                    }
                    else {
                        elevator.setLastFloorToGoTo(highestFloorWithPeople);
                    }
                }
                else {
                    short lowestFloor = elevator.getLastFloorToGoTo();
                    foreach (Person person in people) {
                        if (person.getWantToGoTo() < elevator.getLastFloorToGoTo()) {
                            lowestFloor = person.getWantToGoTo();
                        }
                    }
                    short lowestFloorWithPeople = Interactions.checkFloorsForPeopleBelowElevator(floors, elevator);
                    if (lowestFloor > lowestFloorWithPeople) {
                            elevator.setLastFloorToGoTo(lowestFloor);
                    }
                    else {
                        elevator.setLastFloorToGoTo(lowestFloorWithPeople);
                    }
                }
            }
        }


        //public static short findLastFloorToGoTo(Floor[] floors, short checkFrom, short checkToo, sbyte direction)
        //{
        //    short lastFloorToGoTo = null;
        //    for (int i = checkFrom; i != checkToo; i += direction)
        //    {
        //        List<Person> people = floors[i].getPersonsList();
        //        foreach (Person person in people)
        //        {
        //            if (person.getWantToGoTo() == 0) { }
        //        }
        //    }
        //}


        public static void moveElevators(Building building)
        {
            Elevator[] elevators = building.getElevators();
            foreach(Elevator elevator in elevators) {
                elevator.moveElevator(elevator.getDirection());
            }
        }

        //private short checkForPeopleWantingToGoUpOverCurrentFloor(Floor[] floors, short currentFloor) {
        //    short highestFloorToGoTo = 0;
        //    for (int i = floors.Length-1; i > currentFloor; i--) {
        //        List<Person> people = floors[i].getPersonsList();
        //        foreach(Person person in people) {
        //            if (person.getWantToGoTo() > highestFloorToGoTo) {
        //                highestFloorToGoTo= person.getWantToGoTo();
        //            }
        //        }
        //    }
        //    return highestFloorToGoTo;
        //}

        public static short checkFloorsForPeopleAboveElevator(Floor[] floors, Elevator elevator) {
            for (int i = floors.Length; i > elevator.getCurrentFloor(); i--) {
                List<Person> people = floors[i].getPersonsList();
                if (people.Count() != 0)  {
                    return (short) i;
                }
            }
            return elevator.getCurrentFloor();
        }

        public static short checkFloorsForPeopleBelowElevator(Floor[] floors, Elevator elevator) {
            for (int i = 0; i < elevator.getCurrentFloor(); i++) {
                List<Person> people = floors[i].getPersonsList();
                if (people.Count() != 0)  {
                    return (short) i;
                }
            }
            return elevator.getCurrentFloor();
        }

        public static void changeDirectionElevator(Floor[] floors, Elevator[] elevators)
        {
            foreach (Elevator elevator in elevators)
            {
                if (elevator.getCurrentFloor() == elevator.getLastFloorToGoTo() && elevator.getDirection() > 0)
                {
                    elevator.setLastFloorToGoTo(checkFloorsForPeopleBelowElevator(floors, elevator));
                    if (elevator.getLastFloorToGoTo() == elevator.getCurrentFloor())
                    {
                        elevator.setDirection(0);
                    }
                }
                else if (elevator.getCurrentFloor() == elevator.getLastFloorToGoTo() && elevator.getDirection() < 0)
                {
                    elevator.setLastFloorToGoTo(checkFloorsForPeopleBelowElevator(floors, elevator));
                    if (elevator.getLastFloorToGoTo() == elevator.getCurrentFloor())
                    {
                        elevator.setDirection(0);
                    }
                }
            }
        }
                    
        //public static void checkFloorsForPeopleBelowElevator(Floor[] floors, Elevator elevator)

        //public static void checkElevatorChangeDirection(Building building) {
        //    Elevator[] elevators = building.getElevators();
        //    foreach (Elevator elevator in elevators) {
        //        if (elevator.getCurrentFloor() == elevator.getLastFloorToGoTo()) {
        //            checkForPeopleWantingToGoUpOverCurrentFloor(building.getFloors(), elevator.getCurrentFloor());
        //        }
        //    }
        //{
        //    Elevator[] elevators = building.getElevators();
        //    Floor[] floors = building.getFloors();
        //    foreach (Elevator elevator in elevators)
        //    {
        //        if (elevator.getDirection() < 0)
        //        {
        //            elevator.decrementCurrentFloor(-1);
        //            for (int i = building.getLowestFloor(); i < elevator.getLastFloorToGoTo(); i++)
        //            {
        //                List<Person> people = floors[i].getPersonsList();
        //                foreach (Person person in people)
        //                {
        //                    if (person.getWantToGoTo() < elevator.getLastFloorToGoTo())
        //                    {
        //                        elevator.setLastFloorToGoTo(person.getWantToGoTo());
        //                    }
        //                }
        //            }
        //        }
        //        else if (elevator.getDirection() > 0)
        //        {
        //            elevator.incrementCurrentFloor(1);
        //            for (int i = building.getHighestFloor(); i > elevator.getLastFloorToGoTo(); i--)
        //            {
        //                List<Person> people = floors[i].getPersonsList();
        //                foreach (Person person in people)
        //                {
        //                    if (person.getWantToGoTo() < elevator.getLastFloorToGoTo())
        //                    {
        //                        elevator.setLastFloorToGoTo(person.getWantToGoTo());
        //                    }
        //                }
        //            }
        //        }

        //        else if (elevator.getNumberOfPeopleInElevator() == 0 && elevator.getDirection() > 0)
        //        {
        //            for (int i = building.getHighestFloor(); i > elevator.getCurrentFloor(); i--)
        //            {
        //                List<Person> people = floors[i].getPersonsList();
        //                foreach (Person person in people)
        //                {
        //                    if (person.getWantToGoTo() > elevator.getLastFloorToGoTo())
        //                    {
        //                        elevator.setLastFloorToGoTo(person.getWantToGoTo());
        //                        elevator.setDirection(1);
        //                    }
        //                }
        //            }
        //            if (elevator.getCurrentFloor() == elevator.getLastFloorToGoTo())
        //            {
        //                for (int i = building.getLowestFloor(); i < elevator.getCurrentFloor(); i++)
        //                {
        //                    List<Person> people = floors[i].getPersonsList();
        //                    foreach (Person person in people)
        //                    {
        //                        if (person.getWantToGoTo() < elevator.getLastFloorToGoTo())
        //                        {
        //                            elevator.setLastFloorToGoTo(person.getWantToGoTo());
        //                            elevator.setDirection(1);
        //                        }
        //                    }
        //                }

        //            }

        //            else if (elevator.getNumberOfPeopleInElevator() == 0 && elevator.getDirection() < 0)
        //            {
        //                for (int i = building.getLowestFloor(); i < elevator.getCurrentFloor(); i++)
        //                {
        //                    List<Person> people = floors[i].getPersonsList();
        //                    foreach (Person person in people)
        //                    {
        //                        if (person.getWantToGoTo() < elevator.getLastFloorToGoTo())
        //                        {
        //                            elevator.setLastFloorToGoTo(person.getWantToGoTo());
        //                            elevator.setDirection(-1);
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //    }
        //}
    }
}

